package br.unb.plugins.geral.model;

import java.util.HashMap;

import br.unb.statistic.Generator;
import br.unb.utils.Console;
import br.unb.utils.Resource;

public class GeneratorModel {

    private String classPath;
    private HashMap parametersMap;

    public GeneratorModel(String className) {
        this.parametersMap = new HashMap();
        this.classPath = className;
    }

    public Generator getGenerator() {
        try {
            Generator generator = (Generator) (Class.forName(this.classPath).newInstance());
            generator.setParameters(this.parametersMap);
            /*
            //TODO fix generator interface so it also validates the parameters
            if(generator instanceof GenericDistribution){
            ((GenericDistribution)generator).hasValidParam(desParams);
            }*/
            return generator;
        } catch (InstantiationException e) {
            Console.println(
                    Resource.getString("THE_CLASS")
                    + this.classPath
                    + Resource.getString("DO_NOT_HAVE_EMPTY_CONSTRUCTOR"));
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            Console.println(
                    Resource.getString("COULD_NOT_ACCESS_CLASS")
                    + this.classPath);
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            Console.println(
                    Resource.getString("THE_CLASS")
                    + this.classPath
                    + Resource.getString("NOT_FOUND")
                    + Resource.getString("SUGESTION_VERIFY_XML"));
            e.printStackTrace();
        } catch (Exception e) {
            Console.println(
                    Resource.getString("ERROR")
                    + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public void setParameter(String alias, Object value) {
        this.parametersMap.put(alias, value);
    }

    public void clearParameters() {
        this.parametersMap.clear();
    }

    /*
    public String getParameter(String parameterAlias){
    return (String)this.parametersMap.get(parameterAlias);
    }

    public long getLong(String alias){
    try {
    return Long.parseLong((String)this.parametersMap.get(alias));
    } catch (NumberFormatException e) {
    e.printStackTrace();
    return 0L;
    }
    }
    public double getDouble(String alias){
    try {
    return Double.parseDouble((String)this.parametersMap.get(alias));
    } catch (NumberFormatException e) {
    e.printStackTrace();
    return 0.0d;
    }
    }
     */
}
